/*
 * user_thread.h
 *
 *  Created on: Apr 2, 2012
 *      Author: ariel
 */

#ifndef USER_THREAD_H_
#define USER_THREAD_H_

#include <functional>
#include <setjmp.h>
#include <sys/time.h>

#include "uthreads.h"
#include "common_defs.h"

#define MIN_VAL_PRIORITY (MAX_PRI + 1)

typedef enum _state_t
{
	THREAD_STATE_UNDEFIEND			,
	THREAD_STATE_RUNNING  			,
	THREAD_STATE_READY 		   		,
	THREAD_STATE_SUSPENDED			,
	THREAD_STATE_SLEEP

} thread_state_t;

class UserThread
{
public:

	UserThread( uint id, uint priority );
	~UserThread();

	void set_thread_u_area( sigjmp_buf *jbuf );

	sigjmp_buf* get_thread_jmp_buffer();


	thread_state_t get_thread_state() const;

	result_t set_thread_state(thread_state_t new_state);


	uint get_thread_priority() const;

	uint set_thread_priority(uint new_priority);


	uint get_thread_id() const;

	char* get_stack_addr();


	const timeval *get_ready_time () const;

private:

	thread_state_t 	_state;
	char		_stack[STACK_SIZE];
	uint	 	_priority;
	uint 		_id;
	sigjmp_buf 	_jbuf;
	timeval		_last_time;

	result_t set_ready_time();
};

class ThreadsComparator
{
public:

	bool operator() ( const UserThread* lhs, const UserThread* rhs );
};

#endif /* USER_THREAD_H_ */
